package com.ys.controller.web.finance;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ys.config.MenuId;
import com.ys.config.MyConfig;
import com.ys.dto.SubGridConfigDto;
import com.ys.entity.finance.TcwFinancialTransactionType;
import com.ys.entity.finance.TcwFinancialTransactionTypeItem;
import com.ys.service.sys.CommonApiService;

/**
 * 财务交易类型
 */
@Controller
@RequestMapping("/web/finance/tcwFinancialTransactionType")
public class WebTcwFinancialTransactionTypeController {

	@Autowired
	private CommonApiService<TcwFinancialTransactionType> commonApiService;

    Long menuId = MenuId.TCW_FinancialTransactionType;
    /*
     * 新增
     */
    @GetMapping("/toAdd")
    public String toAdd(Model model) throws Exception{
        // 子列表配置信息
        Map<String,Object> tcwFinancialTransactionType = new HashMap<>();
        tcwFinancialTransactionType.put("status",1);

        TcwFinancialTransactionType entity = new TcwFinancialTransactionType()
                .setCreateTime(new Date());
        entity.setBid(commonApiService.getCommonNextUkey(MenuId.TCW_FinancialTransactionType, "bid"));
        String aa = commonApiService.toAdd(model, entity, MenuId.TCW_FinancialTransactionType, tcwFinancialTransactionType, null);

        if ("test".equals(MyConfig.getActive()) || "pro".equals(MyConfig.getActive())) {
            return "finance/tcwFinancialTransactionType/tcwFinancialTransactionTypeAdd";
		} else {
			return aa;
		}
    }

    /*
     * 编辑
     */
    @GetMapping("/toEdit")
    public String toEdit(Model model, Long id) throws Exception{
        if (id == null){
            return com.ys.vo.W.error(model, "id不能为空");
        }
        List<Map<String, Object>> itemList = new TcwFinancialTransactionTypeItem()
                .setPid(id)
                .setStatus(1)
                .queryMap();
        // 子列表配置信息
        SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MenuId.TCW_FinancialTransactionType, itemList);
        List<SubGridConfigDto.ButtonDto> buttonArr = subGridConfig.getButtonArr();
        List<SubGridConfigDto.ButtonDto> buttonArrNew = new ArrayList<>();

        if (new TcwFinancialTransactionType().setId(id).count() == 0){
            return com.bo.vo.W.error(model, "该信息不存在!");
        }
        for (SubGridConfigDto.ButtonDto dto : buttonArr){
            if (dto.getEvent().equals("event-to-delete")){
                buttonArrNew.add(dto);
            }
            subGridConfig.setButtonArr(buttonArrNew);
        }
        
        String aa = commonApiService.toEdit(model, id, new TcwFinancialTransactionType(), MenuId.TCW_FinancialTransactionType, null, null);
        if ("test".equals(MyConfig.getActive()) || "pro".equals(MyConfig.getActive())) {
            return "finance/tcwFinancialTransactionType/tcwFinancialTransactionTypeEdit";
		} else {
			return aa;
		}
    }
}